import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class XHRServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String action = req.getParameter("action");
        if ("checkUserName".equals(action)) {
            resp.setContentType("text/html;charset=UTF-8");
            String userName = req.getParameter("userName");
            if (userName == null || "".equals(userName.trim())) {
                resp.getWriter().append("<font color='red'>用户名不能为空</font>");
                return;
            }
            userName = new String((userName.getBytes("ISO-8859-1")),"UTF-8");
            System.out.println(userName);
            java.util.ArrayList<String> userNames = new java.util.ArrayList<String>();
            userNames.add("zhangsan");
            userNames.add("lisi");
            userNames.add("wangwu");
            if (userNames.contains(userName)) {
                resp.getWriter().write("<font color='red'>该用户名已经被占用</font>");
            } else {
                resp.getWriter().write("<font color='green'>该用户名可以使用</font>");
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);

    }
}
